package com.gridpower.spreadsheet.client.models;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.TreeMap;

import com.gridpower.spreadsheet.client.models.AppModel.DocInfo;
import com.gridpower.spreadsheet.client.models.AppModel.RemoteFileInfo;

public class DocModel {
	public final static int DEFAULT_CELL_WIDTH = 40;
	public final static int DEFAULT_CELL_HEIGHT = 25;

	protected int docId = 0;// a valid one is an id larger than 0

	protected int rowCount = 'T'-'A' + 1;
	protected int colCount = 100;
	protected int fixedRowCount = 0;
	protected int fixedColCount = 0;

	/**
	 * grid cells map
	 */
	protected TreeMap rowColMap = new TreeMap();
	protected TreeMap colRowMap = new TreeMap();
	
	/**
	 * Add a new cell which the user have manipulate(set some property for the 
	 * GridCell Object)
	 */
	public void AddGridCell(GridCell gc) {
		
	}
	/**
	 * totally initialize the docmodel by a list of GridCells
	 */
	public void initialize(GridCell[] cells) {
		// read file info
		AppModel g = AppModel.getInstance();
		int docId = g.getCurrentDocId();
		RemoteFileInfo fileInfo = g.getFileInfo();
		if (fileInfo != null) {
			DocInfo di = (DocInfo)fileInfo.docInfos.get(fileInfo.docId2Idx(docId));
			this.rowCount = di.rowCount;
			this.colCount = di.colCount;
			this.fixedColCount = di.fixedColCount;
			this.fixedRowCount = di.fixedRowCount;
		}
		
		// read cell info
		for (int i = 0; i < cells.length; ++i) {
			if (cells[i] != null) {
				if (cells[i].row >= 0 && cells[i].col >= 0) {
					TreeMap c = (TreeMap)rowColMap.get(cells[i].row);
					if (c == null) {
						c = new TreeMap();
						rowColMap.put(cells[i].row, c);
					}
					c.put(cells[i].col, cells[i]);
					
					TreeMap r = (TreeMap)rowColMap.get(cells[i].col);
					if (r == null) {
						r = new TreeMap();
						rowColMap.put(cells[i].col, r);
					}
					r.put(cells[i].row, cells[i]);
				}
			}
		}
	}
	public int getColCount() {
		return colCount;
	}
	public TreeMap getColRowMap() {
		return colRowMap;
	}
	public int getDocId() {
		return docId;
	}
	public int getFixedColCount() {
		return fixedColCount;
	}
	public int getFixedRowCount() {
		return fixedRowCount;
	}
	public TreeMap getRowColMap() {
		return rowColMap;
	}
	public int getRowCount() {
		return rowCount;
	}
	public void setColCount(int colCount) {
		this.colCount = colCount;
	}
	public void setColRowMap(TreeMap colRowMap) {
		this.colRowMap = colRowMap;
	}
	public void setDocId(int docId) {
		this.docId = docId;
	}
	public void setFixedColCount(int fixedColCount) {
		this.fixedColCount = fixedColCount;
	}
	public void setFixedRowCount(int fixedRowCount) {
		this.fixedRowCount = fixedRowCount;
	}
	public void setRowColMap(TreeMap rowColMap) {
		this.rowColMap = rowColMap;
	}
	public void setRowCount(int rowCount) {
		this.rowCount = rowCount;
	}
	
}
